using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Text;

namespace Universe.Remoting
{
    public class ServerRepository
    {
        internal List<Item> Items = new List<Item>();

        public ServerRepository Register<C, S>(S service) where S : class, C
        {
            Item i = new Item(typeof(C), service);
            Items.Add(i);
            return this;
        }

        public ServerRepository Register<C, S>() where S : class, C
        {
            Item i = new Item(typeof(C), typeof(S));
            Items.Add(i);
            return this;
        }

        public ServerRepository UnRegister<Contract>()
        {
            List<Item> found = Items.FindAll(
                delegate(Item obj)
                    {
                        return obj.Contract == typeof (Contract);
                    });

            foreach (Item item in found)
            {
                Items.Remove(item);
            }

            return this;
        }

        internal class Item
        {
            public readonly Type Contract;
            public readonly object Singleton;
            public readonly Type Implementation;

            public Item(Type contract, object singleton)
            {
                Contract = contract;
                Singleton = singleton;
            }


            public Item(Type contract, Type implementation)
            {
                Contract = contract;
                Implementation = implementation;
            }

            public WellKnownObjectMode Kind
            {
                get
                {
                    return Singleton != null ? WellKnownObjectMode.Singleton : WellKnownObjectMode.SingleCall;
                }
            }
        }
    }
}